package com.burgeon.backend.EnterpriseWeChat.rest.VipInfo;

import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class findVipInfoById extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public findVipInfoById(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Vip getData(String id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(id), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        Vip vip = new Vip();

        vip.setId(jsonArray.getJSONArray(0).getString(0));
        vip.setVipName(jsonArray.getJSONArray(0).getString(1));
        vip.setCardNo(jsonArray.getJSONArray(0).getString(2));
        if("null".equals(jsonArray.getJSONArray(0).getString(3))){
            vip.setLogoImg("http://jxfs.cloudsmicro.com:8080/img/EnterpriseWeChat/vip/app.png");
        }else {
            vip.setLogoImg(jsonArray.getJSONArray(0).getString(3));
        }
        vip.setOpenid(jsonArray.getJSONArray(0).getString(4));
        vip.setPhone(jsonArray.getJSONArray(0).getString(5));
        vip.setVipType(jsonArray.getJSONArray(0).getString(6));
        vip.setSex(jsonArray.getJSONArray(0).getString(7));
        vip.setBirthday(jsonArray.getJSONArray(0).getString(8));

        return vip;
    }

    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("ID");
        colums.add("VIPNAME");//vip姓名
        colums.add("CARDNO");//卡号
        colums.add("LOGO_URL");//头像
        colums.add("OPENID2");
        colums.add("MOBIL");
        colums.add("'C_VIPTYPE_ID;NAME'");//Vip
        colums.add("SEX");
        colums.add("BIRTHDAY");//

        params.put("columns", colums);
        params.put("table", "C_VIP");//表名

        params.put("count",true);


        HashMap paramss = new HashMap();

        paramss.put("column","ID");
        paramss.put("condition","'"+id+"'");

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) throws Exception {
        findVipInfoById g = new findVipInfoById("39.104.240.134:55321", "nea@burgeon.com.cn", "root55321");
        System.err.println(g.getData("421345"));
    }
}
